﻿using UnityEngine;
using System.Collections;
public class BaseControl : ModuleComponent<BaseControl> ,IGameControl {
	int waitForAframe = 0;
	bool preLoadFinish;
	Loading_View _preLoadView;
	PreLoad _preLoad;
	void Awake(){
		InitModuleName ();
		InitPreLoad();
		Init ();
		OnPreLoadBegin ();
		StartCoroutine (StartLoad ());
	}
	/// <summary>
	/// 初始化模块名字 主要用来做 预加载之类的
	/// </summary>
	protected virtual void InitModuleName(){
	}
	/// <summary>
	/// 初始化预加载
	/// </summary>
	protected virtual void InitPreLoad(){
		_preLoadView = ViewUIMgr.Instance.ShowViewUI<Loading_View>(ViewUIPanelDepth.Top,"Base");
		_preLoadView.SetValue (0);
		_preLoadView._onClose += OnPreLoadFinish;
		_preLoad = new PreLoad (BaseModel.Instance.ModueName);
	}
	IEnumerator  StartLoad(){
		if (_preLoad._dataCount == 0) {
			_preLoadView.SetValue (1);
			yield return null;
		} else {
			for (int i = 0; i < _preLoad._dataCount; i++) {
				_preLoad.Load (i);
				_preLoadView.SetValue (_preLoad._nowLoadValue);
				yield return null;
			}
		}
	}
	public void AddPreLoadData(string path,string name,bool isui){
		_preLoad.AddData (path,name,isui);
	}
	public void GatherPreLoadData(){
		_preLoad.RestCount ();
	}
	/// <summary>
	/// 初始化只允许初始化自身内部数据
	/// </summary>
	public override void Init(){
		base.Init ();
	}
	public virtual void onStart(){
	}
	/// <summary>
	/// 当预加载开始的时候 会调用 这个 如果有非配置表预加载的东西 通过这里添加
	/// </summary>
	public virtual void OnPreLoadBegin(){
		GatherPreLoadData();
	}
	/// <summary>
	/// 预加载完毕的时候调用这个
	/// </summary>
	public virtual void OnPreLoadFinish(){
		preLoadFinish = true;
	}
	public GameObject getObj(){
		return gameObject;
	}
	public void LoadScene(string loadscene){
		FGameControl.Clear ();
		FGameView.Clear ();
		UnityEngine.SceneManagement.SceneManager.LoadScene (loadscene);
	}

	protected void AddPreLoad(string path,string name,bool isui){
		BaseGameMain.Instance.AddPreLoadData (path, name, isui);
	}

	void Update ()
	{
		if (preLoadFinish) {
			if (waitForAframe < 2) {
				waitForAframe++;
				if (waitForAframe == 2)
					WaitForAframe ();
			}
		}
	}
	/// <summary>
	/// 如果需要处理 Start 之后一帧的事 在这处理
	/// </summary>
	protected virtual void WaitForAframe(){
		
	}
	public virtual void Execute(System.Enum command){

	}

	#region LuaCall
	#endregion
}

